<template>
  <div>
    <el-card>
      <div>
        <el-form>
          <el-form-item>
            <el-input v-model="user.username" placeholder="用户名"></el-input>
          </el-form-item>

          <el-form-item>
            <el-input
              show-password
              v-model="user.password"
              placeholder="密码"
            ></el-input>
          </el-form-item>
        </el-form>
      </div>

      <template #footer>
        <el-button @click="reg" type="success">
          <el-icon style="margin-right: 0.3em"><Plus /></el-icon>
          注册</el-button
        >
      </template>
    </el-card>
  </div>
</template>

<script setup>
import { api } from '../js/ajax'
import { ElMessageBox } from 'element-plus'
import { ref } from 'vue'

const emits = defineEmits(['reg-success'])

const loading = ref(false)
const user = ref({
  username: '',
  password: '',
})

const reg = () => {
  loading.value = true
  api.post('/user', user.value, data => {
    loading.value = false
    if (data.success) {
      ElMessageBox.alert(data.message, '注册成功')
        .then(() => {
          emits('reg-success')
        })
        .catch(() => {
          emits('reg-success')
        })
    } else {
      ElMessageBox.alert(data.message, '注册失败')
    }
  })
}
</script>

<style scoped></style>
